home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / test / utiltest.py < prev   
Encoding:
Python Source  |  2007-11-12  |  3.6 KB  |  109 lines

  1. from StringIO import StringIO
  2.  
  3. import os
  4. import tempfile
  5.  
  6. from test.framework import DemocracyTestCase
  7. import download_utils
  8. import util
  9. import xhtmltools
  10.  
  11. class FakeStream:
  12.     """Fake streams are used for the AutoflushingStream test.  They don't
  13.     really do much, except check that write is always called with a string
  14.     object (unicode won't always work when writing to stdout).
  15.     """
  16.  
  17.     def write(self, out):
  18.         if not isinstance(out, str):
  19.             raise ValueError("Got non-string object (%s) from "
  20.             "autoflushing stream" % str.__class__)
  21.     def flush(self):
  22.         pass
  23.  
  24. class AutoflushingStreamTest(DemocracyTestCase):
  25.     def setUp(self):
  26.         DemocracyTestCase.setUp(self)
  27.         self.stream = FakeStream()
  28.         self.afs = util.AutoflushingStream(self.stream)
  29.  
  30.     def testBasicWrite(self):
  31.         self.afs.write("Hello World\n")
  32.         self.afs.write("")
  33.         self.afs.write("LotsofData" * 200)
  34.  
  35.     def testUnicodeWrite(self):
  36.         self.afs.write(u'\xf8')
  37.  
  38. class UtilTest(DemocracyTestCase):
  39.     def testAbsolutePathToFileURL(self):
  40.         testPaths = {
  41.             '/ben/dean/kawamura' : 'file:///ben/dean/kawamura',
  42.             '/eight/bit/path/\xe4' : 'file:///eight/bit/path/%E4',
  43.             u'/unicode/path/\xe4' : 'file:///unicode/path/%C3%A4',
  44.         }
  45.         for source, target in testPaths.items():
  46.             self.assertEquals(util.absolutePathToFileURL(source), target)
  47.  
  48.     def testStringify(self):
  49.         # input, handleerror, expected output
  50.         # if handlerror is None, then it isn't passed in as an argument
  51.         t = [
  52.               ( "", None, ""),
  53.               ( "abc", None, "abc"),
  54.               ( 5, None, "5"),
  55.               ( 5.5, None, "5.5"),
  56.               ( u"abc", None, "abc"),
  57.               ( u"abc\xe4", None, "abcä"),
  58.               ( u"abc\xe4", "replace", "abc?")
  59.             ]
  60.  
  61.         for i, h, o in t:
  62.             if h == None:
  63.                 self.assertEquals(util.stringify(i), o)
  64.             else:
  65.                 self.assertEquals(util.stringify(i, h), o)
  66.  
  67.     def testRandomString(self):
  68.         ret = util.random_string(0)
  69.         self.assertEquals(len(ret), 0)
  70.  
  71.         for length in (1, 5, 10):
  72.             ret = util.random_string(length)
  73.             self.assertEquals(len(ret), length)
  74.             self.assertEquals(ret.isalpha(), True)
  75.  
  76. class XHTMLToolsTest(DemocracyTestCase):
  77.     def testMultipartEncode(self):
  78.         vars = {
  79.                 'foo': u'123',  # unicode string
  80.         }
  81.  
  82.         files = {
  83.             'baz': {"filename":"binarydata.zip",
  84.                  "mimetype":"application/octet-stream",
  85.                  "handle": StringIO('\xf8'), 
  86.              } # baz has invalid unicode data
  87.         }
  88.  
  89.         boundary, data = xhtmltools.multipartEncode(vars, files)
  90.  
  91. class DownloadUtilsTest(DemocracyTestCase):
  92.     def checkCleanFilename(self, filename, test_against):
  93.         self.assertEquals(download_utils.cleanFilename(filename),
  94.                 test_against)
  95.  
  96.     def testCleanFilename(self):
  97.         self.checkCleanFilename('normalname', 'normalname')
  98.         self.checkCleanFilename('a:b?c>d<e|f*/g\\h"\'', 'abcdefgh')
  99.         self.checkCleanFilename('', '_')
  100.         longFilename = 'booya' * 100
  101.         longExtension = '.' + 'foo' * 20
  102.         self.checkCleanFilename(longFilename, longFilename[:100])
  103.         # total file length isn't over the limit, so the extension stays the
  104.         # same
  105.         self.checkCleanFilename('abc' + longExtension, 
  106.             'abc' + longExtension)
  107.         self.checkCleanFilename(longFilename + longExtension,
  108.             longFilename[:50] + longExtension[:50])
  109.